#include "SDLAudio.h"

SDLAudio* SDLAudio::instance = nullptr;

SDLAudio::SDLAudio() :
	audio_rate(22050),
	audio_format(AUDIO_S16),
	audio_channels(2),
	audio_buffers(1024)
{}


SDLAudio::~SDLAudio()
{
	Mix_CloseAudio();
	Mix_Quit();
}

void SDLAudio::Init()
{
	Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers);

	SetVolume(BASE_VOLUME);

	if(Mix_Init(MIX_INIT_FLAC|MIX_INIT_OGG))
	{
		std::cout << Mix_GetError() << std::endl;
	}

	Mix_AllocateChannels(4);
	Mix_SetPosition(LEFT, 270, 0);
	Mix_SetPosition(RIGHT, 90, 0);
}

void SDLAudio::PlayMusic(Mix_Music* music)
{
	PlayMusic(music, 0);
}

void SDLAudio::PlayMusic(Mix_Music* music, int loop)
{
	Mix_PlayMusic(music, loop);
}

void SDLAudio::PlaySound(Mix_Chunk* sound, SoundRegion region)
{
	if(region == LOOP)
	{
		Mix_PlayChannel(region, sound, LOOP_FOREVER);
		
	}
	else
	{
		Mix_PlayChannel(region, sound, 0);
	}
}

void SDLAudio::StopMusic()
{
	Mix_HaltMusic();
}